/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.databaseproject.persistence;

import com.nanosn.coffeemaker.Project;
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
import org.apache.openjpa.lib.log.AbstractLog;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.lib.log.LogFactory;

/**
 *
 * @author selkhateeb
 */
public class TemporaryJDBCConfiguration extends JDBCConfigurationImpl {

    public TemporaryJDBCConfiguration() {
        //TODO read all JDBC settings from settings file
        this.setConnectionDriverName("org.apache.derby.jdbc.EmbeddedDriver");
        this.setConnectionURL("jdbc:derby:" + Project.TMP + "/db.db;create=true;logDevice=" + Project.TMP);
        this.setMetaDataFactory("jpa");
        this.setLogFactory(new CustomLogFactory());
//        this.setDBDictionary("derby");

    }
}

class CustomLogFactory
        implements LogFactory {

    private String _prefix = "CUSTOM LOG";

    public void setPrefix(String prefix) {
        _prefix = prefix;
    }

    public Log getLog(String channel) {
        // Return a simple extension of AbstractLog that will log
        // everything to the System.err stream. Note that this is
        // roughly equivalent to OpenJPA's default logging behavior.
        return new AbstractLog() {

            protected boolean isEnabled(short logLevel) {
                // disable all levels
                return false;
            }

            protected void log(short type, String message, Throwable t) {
                // just send everything to System.err
                System.err.println(_prefix + ": " + type + ": "
                                   + message + ": " + t);
            }
        };
    }
}
